package com.rest.demo.nio;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;

/**
 * @author Administrator
 * @decription
 * @Date 2018-02-07 16:20
 */
public class BioSocketDemo {
    public static void main(String[] args) throws IOException {
        server(8080);
    }

    public static void server(int port) throws IOException {
        final ServerSocket socket = new ServerSocket();
        while(true) {
            final Socket clientSocket = socket.accept();
            System.out.println("Accepted connection from" + clientSocket);
            new Thread(() -> {
                OutputStream out = null;
                try {
                    out = clientSocket.getOutputStream();
                    out.write("Hi\r\n".getBytes(Charset.forName("UTF-8")));
                    out.flush();
                    clientSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }
    }
}
